package net.wanglu.www.zzz.service.se.nsubj;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import net.wanglu.www.zzz.service._Service;
import net.wanglu.www.zzz.service.se.__SentenceElements;
import edu.stanford.nlp.trees.EnglishGrammaticalRelations;
import edu.stanford.nlp.trees.GrammaticalRelation;
import edu.stanford.nlp.trees.TypedDependency;

public class _NSubjExtracter extends _Service {

	private _Service commonNSubjExtracter = null;
	private _Service commonDeclarativeNSubjExtracter = null;
	private _Service explNSubjExtracter = null;
	private _Service rcmodNSubjExtracter = null;
	
	public _Service getCommonNSubjExtracter() {
		return commonNSubjExtracter;
	}
	public void setCommonNSubjExtracter(_Service commonNSubjExtracter) {
		this.commonNSubjExtracter = commonNSubjExtracter;
	}
	public _Service getCommonDeclarativeNSubjExtracter() {
		return commonDeclarativeNSubjExtracter;
	}
	public void setCommonDeclarativeNSubjExtracter(
			_Service commonDeclarativeNSubjExtracter) {
		this.commonDeclarativeNSubjExtracter = commonDeclarativeNSubjExtracter;
	}
	public _Service getExplNSubjExtracter() {
		return explNSubjExtracter;
	}
	public void setExplNSubjExtracter(_Service explNSubjExtracter) {
		this.explNSubjExtracter = explNSubjExtracter;
	}
	public _Service getRcmodNSubjExtracter() {
		return rcmodNSubjExtracter;
	}
	public void setRcmodNSubjExtracter(_Service rcmodNSubjExtracter) {
		this.rcmodNSubjExtracter = rcmodNSubjExtracter;
	}

	@Override
	public Object service(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		__SentenceElements se = null;
		if ( objects.length >1) {
			se = (__SentenceElements) objects[1];
		} else {
			se = new __SentenceElements();
		}
		
		ArrayList<_NSubjTuple> NSubjTupleList = new ArrayList<_NSubjTuple>(8);
		Collection<?> tdl = (Collection<?>) preprocessData.get("tdl");
		Set<String> used = new HashSet<String>(16);
		for( Object obj : tdl) {
			TypedDependency TD = (TypedDependency) obj;
			GrammaticalRelation reln = TD.reln();
			if ( reln.equals(EnglishGrammaticalRelations.EXPLETIVE)) {
				/*
				 * 0.将gov存储以来，防止后面的CommonNsubj重复处理
				 * 1.调用expl主谓解析器
				 * 2.将主谓结果添加到NSubjTupleList
				 */
				used.add(TD.gov().toString());
				_NSubjTuple nt = (_NSubjTuple) this.explNSubjExtracter.service(preprocessData, TD, se.ExplTupleList);
				if ( nt != null) NSubjTupleList.add(nt);
			} else if ( reln.equals(EnglishGrammaticalRelations.RELATIVE_CLAUSE_MODIFIER)) {
				/*
				 * 0.将gov存储以来，防止后面的CommonNsubj重复处理
				 * 1.调用rcmod主谓解析器
				 * 2.将主谓结果添加到NSubjTupleList
				 */
				used.add(TD.dep().toString());
				_NSubjTuple nt = (_NSubjTuple) this.rcmodNSubjExtracter.service(preprocessData, TD, se.RcmodTupleList);
				if ( nt != null) NSubjTupleList.add(nt);
			}
		}

		for( Object obj : tdl) {
			TypedDependency TD = (TypedDependency) obj;
			GrammaticalRelation reln = TD.reln();
			if ( reln.equals(EnglishGrammaticalRelations.NOMINAL_SUBJECT) || reln.equals(EnglishGrammaticalRelations.NOMINAL_PASSIVE_SUBJECT)) {
				/*
				 * 0.判断gov是否已经被其他两个解析器处理过
				 * 1.如果没有调用common主谓解析器
				 * 2.将主谓结果添加到NSubjTupleList
				 */
				if ( !used.contains(TD.gov().toString())) {
					_NSubjTuple nt = (_NSubjTuple) this.commonNSubjExtracter.service(preprocessData, TD);
					if ( nt != null) NSubjTupleList.add(nt);					
				}
			}
		}
		se.NSubjTupleList.addAll(NSubjTupleList);
		return se;
	}
}
